﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace XNAGameFeatures.Input.Device
{
    public enum MouseButton
    {
        Left,
        Right,
        Center,
        XButton1,
        XButton2
    }

    public class MouseInput : InputDevice
    {

        #region BaseInput

        private MouseState _current;
        private MouseState _old;
        public Vector2 MousePosition { get; set; }
        public int ScrollWheelValue { get; set; }
        public int LastScrollWheelValue { get; set; }

        internal MouseInput()
        {

        }

        /// <summary>
        /// Updates all input entry.
        /// </summary>
        internal override void Update()
        {
            _old = _current;
            _current = Mouse.GetState();

            MousePosition = new Vector2(_current.X, _current.Y);
            ScrollWheelValue = _current.ScrollWheelValue;
            LastScrollWheelValue = _old.ScrollWheelValue;
        } 

        #endregion

        #region Public Methods

        private bool MouseButtonDown(MouseButton button)
        {
            switch (button)
            {
                case MouseButton.Left:
                    return _current.LeftButton == ButtonState.Pressed;
                case MouseButton.Right:
                    return _current.RightButton == ButtonState.Pressed;
                case MouseButton.Center:
                    return _current.MiddleButton == ButtonState.Pressed;
                case MouseButton.XButton1:
                    return _current.XButton1 == ButtonState.Pressed;
                case MouseButton.XButton2:
                    return _current.XButton2 == ButtonState.Pressed;
            }
            return false;
        }

        private bool MouseButtonUp(MouseButton button)
        {
            switch (button)
            {
                case MouseButton.Left:
                    return _old.LeftButton == ButtonState.Released;
                case MouseButton.Right:
                    return _old.RightButton == ButtonState.Released;
                case MouseButton.Center:
                    return _old.MiddleButton == ButtonState.Released;
                case MouseButton.XButton1:
                    return _old.XButton1 == ButtonState.Released;
                case MouseButton.XButton2:
                    return _old.XButton2 == ButtonState.Released;
            }
            return false;
        }

        /// <summary>
        /// Determines whether [is button press once] [the specified button].
        /// </summary>
        /// <param name="button">The button.</param>
        /// <returns>
        ///   <c>true</c> if [is button press once] [the specified button]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsButtonPressOnce(MouseButton button)
        {
            return MouseButtonDown(button) && MouseButtonUp(button);
        }

        /// <summary>
        /// Determines whether [is button press] [the specified button].
        /// </summary>
        /// <param name="button">The button.</param>
        /// <returns>
        ///   <c>true</c> if [is button press] [the specified button]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsButtonPress(MouseButton button)
        {
            return MouseButtonDown(button);
        }

        #endregion

    }
}
